﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class InputManager : MonoBehaviour {

	private GameObject selectionBox;
	private GameObject selectedObject;

	private Vector2 mouseDownPos;
	private Vector2 mouseUpPos;
	private Vector2 currentSwipe;

	public static List<GameObject> swipeables = new List<GameObject> ();
	

	void Start()
	{
		selectionBox = GameObject.Find ("selection");
	}
	 
	void Update () 
	{
		HandleSelection ();
		DetectSwipe ();

	}

	private void HandleSelection()
	{
		if (Input.GetMouseButtonDown(0))
		{
			RaycastHit hitInfo = new RaycastHit();
			
			if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, Mathf.Infinity, 1 << LayerMask.NameToLayer("Clickable")))
			{
				selectionBox.transform.parent = hitInfo.transform;
				selectionBox.transform.position = hitInfo.transform.position;
				selectedObject = hitInfo.transform.gameObject;
				Debug.Log("hit");
			}
			else 
			{
				Debug.Log("miss");
				Vector3 vec = new Vector3(100f,100f,100f);
				selectionBox.transform.position = vec;
				selectedObject = null;
			}
		}
	}

	private void DetectSwipe()
	{
		if(Input.GetMouseButtonDown(0))
		{
			mouseDownPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
		}
		if(Input.GetMouseButtonUp(0))
		{
			mouseUpPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);

			currentSwipe = new Vector2(mouseUpPos.x - mouseDownPos.x, mouseUpPos.y - mouseDownPos.y);
			
			//normalize the 2d vector
			currentSwipe.Normalize();

			//swipe upwards
			if(currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
			{
				Debug.Log("up swipe");
			}
			//swipe down
			if(currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
			{
				Debug.Log("down swipe");
			}
			//swipe left
			if(currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
			{
				Debug.Log("left swipe");
			}
			//swipe right
			if(currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
			{
				Debug.Log("right swipe");
			}

		}

		if (Input.GetMouseButton (0)) 
		{
			mouseUpPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
			
			currentSwipe = new Vector2(mouseUpPos.x - mouseDownPos.x, mouseUpPos.y - mouseDownPos.y);
			
			//normalize the 2d vector
			currentSwipe.Normalize();
			
			//swipe upwards
			if(currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
			{
				Debug.Log("up swipe: in progress");
			}
			//swipe down
			if(currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
			{
				Debug.Log("down swipe: in progress");
			}
			//swipe left
			if(currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
			{
				Debug.Log("left swipe: in progress");
			}
			//swipe right
			if(currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
			{
				Debug.Log("right swipe: in progress");
			}
		}
	}


}
